#include "interface.h"



int mW_interface_draw (BITMAP * surface)
{
	
	mW_interface_textbox * textbox;
	mW_interface_selectbox * selectbox;
	char * option_text;
	
	int selectbox_option_i;
	int selectbox_option_height;
	
	
	
	mW_list_reset_iterator(mW_interface_textboxes, -1);
	
	textbox = mW_list_iterate(mW_interface_textboxes, -1);
	while (textbox != NULL)
	{
		
		mW_interface_draw_textbox(surface, textbox);
		
		textbox = mW_list_iterate(mW_interface_textboxes, -1);
		
	}
	
	
	
	textbox = (mW_interface_textbox *) malloc(sizeof(mW_interface_textbox));
	if (textbox == NULL)
		printf("Error allocating memory for textbox\n");
	
	mW_list_reset_iterator(mW_interface_selectboxes, -1);
	
	selectbox = mW_list_iterate(mW_interface_selectboxes, -1);
	while (selectbox != NULL)
	{
		
		mW_list_reset_iterator(selectbox->options, 1);
			
		selectbox_option_height = text_height(font);
		
		selectbox_option_i = 0;
		option_text = mW_list_iterate(selectbox->options, 1);
		while (option_text != NULL)
		{
			
			textbox->type = (unsigned char) 1;
			textbox->left = selectbox->left;
			textbox->top = selectbox->top +  selectbox_option_i * selectbox_option_height + selectbox_option_i * 9;
			textbox->width = selectbox->width + 7;
			textbox->height = selectbox_option_height + 7;
			textbox->text = option_text;
			
			mW_interface_draw_textbox(surface, textbox);
			
			selectbox_option_i++;
			option_text = mW_list_iterate(selectbox->options, 1);
			
		}
		
		selectbox = mW_list_iterate(mW_interface_selectboxes, -1);
		
	}
		
	free (textbox);
	
	return 1;
	
}



int mW_interface_draw_textbox (BITMAP * surface, mW_interface_textbox * textbox)
{
	
	int textbox_border_color;
	int textbox_background_color;
	int textbox_over_background_color;
	int textbox_text_color;
	
	textbox_border_color = makecol(0xCC, 0xCC, 0xCC);
	textbox_background_color = makecol(0x33, 0x33, 0x33);
	textbox_over_background_color = makecol(0x66, 0x00, 0x00);
	textbox_text_color = makecol(0xFF, 0xFF, 0xFF);
	
	
	
	acquire_bitmap(surface);
	
	line(surface, textbox->left, textbox->top, textbox->left + textbox->width, textbox->top, textbox_border_color);
	line(surface, textbox->left, textbox->top, textbox->left, textbox->top + textbox->height, textbox_border_color);
	line(surface, textbox->left + textbox->width, textbox->top, textbox->left + textbox->width, textbox->top +  textbox->height, textbox_border_color);
	line(surface, textbox->left, textbox->top +  textbox->height, textbox->left + textbox->width, textbox->top + textbox->height, textbox_border_color);
		
	if ((textbox->type & 0xF) == 1)
	{
		
		if ((mouse_x >= textbox->left) && (mouse_x <= textbox->left + textbox->width)
		   && (mouse_y >= textbox->top) && (mouse_y <= textbox->top + textbox->height))
		{
			rectfill(surface,
					 textbox->left + 1,
					 textbox->top + 1,
					 textbox->left + textbox->width - 1,
					 textbox->top + textbox->height - 1,
					 textbox_over_background_color);
			textout_ex(surface, font, textbox->text, textbox->left + 4, textbox->top + 4, textbox_text_color, textbox_over_background_color);
		}
		else
		{
			rectfill(surface,
					 textbox->left + 1,
					 textbox->top + 1,
					 textbox->left + textbox->width - 1,
					 textbox->top + textbox->height - 1,
					 textbox_background_color);
			textout_ex(surface, font, textbox->text, textbox->left + 4, textbox->top + 4, textbox_text_color, textbox_background_color);
		}
		
	}
	else
	{
		
		rectfill(surface,
				 textbox->left + 1,
				 textbox->top + 1,
				 textbox->left + textbox->width - 1,
				 textbox->top + textbox->height - 1,
				 textbox_background_color);
		textout_ex(surface, font, textbox->text, textbox->left + 4, textbox->top + 4, textbox_text_color, textbox_background_color);
	}
		
	release_bitmap(surface);
		
	return 1;
	
}



int mW_interface_init()
{
	
	mW_interface_textboxes = mW_list_create();
	mW_interface_selectboxes = mW_list_create();
	mW_interface_selectbox_events = mW_list_create();
	
	if ((mW_interface_textboxes == NULL) || (mW_interface_selectboxes == NULL)
	    || (mW_interface_selectbox_events == NULL))
		return 0;
	
	return 1;

}



/*
 * DO NOT PUT THIS IN AN INTTERUPT WHERE IT COULD BE CALLED IN THE
 *   MIDDLE OF mW_interface_draw()
 */
int mW_interface_input ()
{
	
	mW_mouse_click * mouse_click;
	mW_interface_textbox * textbox;
	mW_interface_selectbox * selectbox;
	mW_interface_selectbox_event * selectbox_event;
	char * selectbox_option;
	int selectbox_option_i;
	int selectbox_option_height;
	
	
	mW_mouse_reset_iterator();
	mouse_click = mW_mouse_get_next_click();
	while (mouse_click != NULL)
	{
		
		if (mouse_click->buttons == 1)
		{
			
			// check textboxes
			mW_list_reset_iterator(mW_interface_textboxes, -1);
			
			textbox = mW_list_iterate(mW_interface_textboxes, -1);
			while (textbox != NULL)
			{
				
				// this textbox is automatically dismissed when clicked
				if ((textbox->type & 0x10) > 0)
				{
					// the mouse clicked this textbox
					if ((mouse_click->x >= textbox->left) && (mouse_click->y <= textbox->left + textbox->width)
						&& (mouse_click->y >= textbox->top) && (mouse_click->y <= textbox->top + textbox->height))
					{
						if(mW_interface_textbox_destroy(mW_list_get_id(mW_interface_textboxes, textbox)) == 0)
							printf("Error destroying automatically dismissed textbox\n");
						mW_mouse_delete_last_click();
						return 1;
					}
				}
				
				textbox = mW_list_iterate(mW_interface_textboxes, -1);
				
			}
			
			// check selectboxes
			mW_list_reset_iterator(mW_interface_selectboxes, -1);
			
			selectbox = mW_list_iterate(mW_interface_selectboxes, -1);
			while (selectbox != NULL)
			{
				
				// the mouse clicked inside this selectbox
				if ((mouse_click->x >= selectbox->left) && (mouse_click->x <= selectbox->left + selectbox->width)
				    && (mouse_click->y >= selectbox->top) && (mouse_click->y <= selectbox->top + selectbox->height))
				{
					
					// iterate through each option
					mW_list_reset_iterator(selectbox->options, 1);
					
					selectbox_option_height = text_height(font);
					selectbox_option_i = 0;
					selectbox_option = mW_list_iterate(selectbox->options, 1);
					while (selectbox_option != NULL)
					{
						
						// if this option was clicked
						// this is a pretty involved if statement. mW_interface_draw() has cleaner code
						//   for determining boundaries of a selectbox option
						if ((mouse_click->x >= selectbox->left) && (mouse_click->x <= selectbox->left + selectbox->width + 7)
						    && (mouse_click->y >= selectbox->top + selectbox_option_i * selectbox_option_height + selectbox_option_i * 9) 
						    && (mouse_click->y <= selectbox->top + (selectbox_option_i + 1) * selectbox_option_height + selectbox_option_i * 9 + 7))
						{
							
							// allocate memory for selectbox event and set it up
							selectbox_event = (mW_interface_selectbox_event *) malloc(sizeof(mW_interface_selectbox_event));
							if (selectbox_event == NULL)
								printf("Error allocating memory for selectbox event\n");
							selectbox_event->selectbox_id = mW_list_get_id(mW_interface_selectboxes, selectbox);
							selectbox_event->option_id = mW_list_get_id(selectbox->options, selectbox_option);
							
							// add selectbox to mW_interface_selectbox_events
							mW_list_insert(mW_interface_selectbox_events, selectbox_event);
							
							// remove mouse click
							mW_mouse_delete_last_click();
							
							return 2;
							
						}
			
					} // while (selectbox_option != NULL)
					
				} // if mouse clicks inside this selectbox
				
				selectbox = mW_list_iterate(mW_interface_selectboxes, -1);
				
			} // while (selectbox != NULL)
			
		} // if (mouse_click->buttons == 1)
		
		mouse_click = mW_mouse_get_next_click();
		
	} // while (mouse_click != NULL)
	
	return 0;
	
}



int mW_interface_selectbox_add_option (int selectbox_id, char * option_text)
{
	
	int t_width = text_length(font, option_text);
	int t_height = text_height(font);
	
	mW_interface_selectbox * selectbox;

	char * text = malloc(strlen(option_text) + 1);
	if (text == NULL)
		printf("Error allocating memory for textbox in selectbox option\n");
	memset(text, 0, strlen(option_text) + 1);
	strcpy(text, option_text);
	
	selectbox = mW_list_get_by_id(mW_interface_selectboxes, selectbox_id);
	
	if (t_width > selectbox->width)
		selectbox->width = t_width;
	
	selectbox->height += t_height + 9;
	
	return mW_list_insert(selectbox->options, text);
	
}



int mW_interface_selectbox_create (int left, int top)
{
	
	mW_interface_selectbox * selectbox;
	
	selectbox = (mW_interface_selectbox *) malloc(sizeof(mW_interface_selectbox));
	if (selectbox == NULL)
		mW_error("Error creating selectbox\n");
	
	selectbox->options = mW_list_create();
	selectbox->left = left;
	selectbox->top = top;
	selectbox->width = 0;
	selectbox->height = 0;
	
	return mW_list_insert(mW_interface_selectboxes, selectbox);
	
}



int mW_interface_selectbox_destroy (int selectbox_id)
{
	
	mW_interface_selectbox * selectbox;
	
	selectbox = mW_list_get_by_id(mW_interface_selectboxes, selectbox_id);
	
	if (selectbox == NULL)
		return 0;
	else
	{
	
		if (mW_list_destroy(selectbox->options) == 0)
		{
			printf("mW_list_destroy(selectbox->options) error\n");
			return 0;
		}
		
	}
	
	return mW_list_delete(mW_interface_selectboxes, selectbox_id);
	
}	



mW_interface_selectbox_event * mW_interface_selectbox_get_event()
{
	
	mW_interface_selectbox_event * selectbox_event;
	
	if (mW_list_get_size(mW_interface_selectbox_events) == 0)
		return NULL;
		
	selectbox_event = (mW_interface_selectbox_event *) malloc(sizeof(mW_interface_selectbox_event));
	if (selectbox_event == NULL)
		return NULL;
		
	memcpy(selectbox_event, mW_list_get_first(mW_interface_selectbox_events), sizeof(mW_interface_selectbox_event));
	
	mW_list_delete_first(mW_interface_selectbox_events);
	
	return selectbox_event;
	
}



/*
 * returns -1 on failure, or the id of the textbox on success
 */
int mW_interface_textbox_create (char * text, int left, int top, unsigned char type)
{
	
	mW_interface_textbox * textbox;
	
	textbox = (mW_interface_textbox *) malloc(sizeof(mW_interface_textbox));
	if (textbox == NULL)
		printf("Error allocated memory for textbox\n");
	
	textbox->text = (char *) malloc(strlen(text) + 1);
	if (textbox->text == NULL)
		printf("Error allocating memory for textbox text\n");
	memset(textbox->text, 0, strlen(text) + 1);
	strcpy(textbox->text, text);
		
	textbox->left = left;
	textbox->top = top;
	textbox->width = text_length(font, text) + 7;
	textbox->height = text_height(font) + 7;
	textbox->type = type;
	
	return mW_list_insert(mW_interface_textboxes, textbox);
	
}



int mW_interface_textbox_destroy (int textbox_id)
{
	
	mW_interface_textbox * textbox;
	
	textbox = mW_list_get_by_id(mW_interface_textboxes, textbox_id);
	
	free(textbox->text);
	
	return mW_list_delete(mW_interface_textboxes, textbox_id);
	
}
